/** 后台-配置列表页面 **/
import Http from '@/common/js/http.js'
import Pagekey from './page_list/pagekey.js'
import Datas from './page_list/data.js'
import Paginate from './page_list/paginate.js'
import Search from './page_list/search.js'
import EduMessage from '@/common/js/message.js';

export default {
  namespaced: true,
  state: {
    responseData: {},
    xUri: null,
    xParams: {}
  },
  modules: {
    pagekey: Pagekey,
    datas: Datas,
    paginate: Paginate,
    search: Search
  },
  mutations: {
    SET_RESPONSE_DATA: (state, data) => {
      state.responseData = data
    }
  },
  actions: {
    page({
      commit,
      dispatch
    }, {
      uri,
      params
    }) {
      dispatch('setLoading', true, {
        root: true
      })
      dispatch('datas/setDatas', []);
      this.xUri = uri;
      this.xParams = params;
      // 设置表单key
      dispatch('pagekey/setPagekeyList', [])
      // 设置搜索字段key
      dispatch('search/setSearchkeyList', [])
      Http.get(uri, params, res => {
        // 返回数据绑定
        commit('SET_RESPONSE_DATA', res.data)
        // 设置列表key
        dispatch('pagekey/setPagekeyList', res.data.pageKey)
        // 设置列表数据
        dispatch('datas/setDatas', res.data.datas);
        // 设置搜索字段key
        dispatch('search/setSearchkeyList', res.data.searchKey)
        // 设置分页
        dispatch('paginate/setPaginate', res.data.paginate)
        dispatch('setLoading', false, {
          root: true
        });
      }, error => {
        EduMessage.error(error.msg);
      }, false)
    },
    // 加载数据操作,分页/搜索
    loadData({
      commit,
      dispatch,
      getters
    }, {
      params: xParams = {}
    }) {
      dispatch('setLoading', true, {
        root: true
      })
      Http.get(this.xUri, {
        ...this.xParams,
        page: getters['paginate/page'],
        count: getters['paginate/count'],
        ...getters['search/data'],
        ...xParams
      }, res => {
        // 返回数据绑定
        commit('SET_RESPONSE_DATA', res.data)
        // 设置列表数据
        dispatch('datas/setDatas', res.data.datas);
        // 设置分页
        dispatch('paginate/setPaginate', res.data.paginate)
        dispatch('setLoading', false, {
          root: true
        });
      }, error => {
        EduMessage.error(error.msg);
      }, false)
    }
  },
  getters: {
    // 返回数据
    response(state) {
      return state.responseData
    }
  }
}
